package cn.xiaoliu.org.controller;

import cn.xiaoliu.basic.utils.AjaxResult;
import cn.xiaoliu.basic.utils.PageList;
import cn.xiaoliu.org.domain.Department;
import cn.xiaoliu.org.domain.DictionaryType;
import cn.xiaoliu.org.query.DepartmentQuery;
import cn.xiaoliu.org.query.DictionaryTypeQuery;
import cn.xiaoliu.org.service.IDictionaryTypeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dictype")
public class DictionaryTypeController {
    @Autowired
    private IDictionaryTypeService dictionaryTypeService;

    //查询全部
    @GetMapping
    public List<DictionaryType> queryAll(){
        return dictionaryTypeService.queryAll();
    }

    //分页加高级查询  post
    @PostMapping("/list")
    public PageList<DictionaryType> queryPage(@RequestBody DictionaryTypeQuery query){
        return dictionaryTypeService.queryPage(query);
    }

    //删除  delete
    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            dictionaryTypeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("业务繁忙，稍后重试");
        }
    }

    //修改新增  put
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody DictionaryType dictionaryType){
        try {
            if (dictionaryType.getId()==null){
                dictionaryTypeService.add(dictionaryType);
            }else {
                dictionaryTypeService.update(dictionaryType);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("业务繁忙，稍后重试");
        }
    }

    //批量删除
    @PatchMapping
    public AjaxResult pachRemove(@RequestBody List<Long> ids){
        try {
            dictionaryTypeService.pachRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("业务繁忙，稍后重试");
        }
    }
}
